package com.zkadmin.controller;

import org.apache.shiro.SecurityUtils;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Button;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Window;

import com.zkadmin.entity.SysRole;
import com.zkadmin.renderer.SysRoleListRenderer;
import com.zkadmin.service.SysRoleService;
import com.zkadmin.util.AdminConstants;
import com.zkadmin.util.AdminWebUtils;
import com.zkadmin.util.PagingWrap;

@SuppressWarnings("serial")
public class RoleListCtrl extends BaseCtrl<Window> {

	@WireVariable
	private SysRoleService sysRoleService;

	private Window win;

	@Wire
	private Listbox roleLst;

	@Wire
	private Paging rolePg;

	@Wire
	private Button createRoleBtn;

	@Override
	protected void init(Window win) {
		this.win = win;
		if (!SecurityUtils.getSubject().hasRole(AdminConstants.ROLE_ADMIN)) {
			createRoleBtn.setDisabled(!SecurityUtils.getSubject().isPermitted("sys:role:create"));
		}

		roleLst.setItemRenderer(new SysRoleListRenderer(win, this, sysRoleService));
		refreshData();
	}

	public void refreshData() {
		new PagingWrap<SysRole>(sysRoleService, roleLst, rolePg).paging();
	}

	@Listen("onClick = #createRoleBtn")
	public void showCreateDialog(Event e) {
		Window window = (Window) Executions.createComponents(AdminWebUtils.getZulPagePath("admin/create_role_dialog.zul"), win, null);
		window.addEventListener(AdminConstants.EVENT_ONREFRESH, new EventListener<Event>() {
			@Override
			public void onEvent(Event event) throws Exception {
				refreshData();
			}
		});
		AdminWebUtils.eventAddCaption(window, e);
		window.doModal();
	}

}
